package com.aide.simplification.mail;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {

	public void sendMail(MailBean mail) throws AddressException,
			MessagingException, UnsupportedEncodingException {
		//stmp主机地址
		String stmp_host = mail.getStmp_host();
		//群发 mail数组 接收者
		String[] tomails = mail.getToMails();
		//附件名数组
		String[] uploadFileName = mail.getUploadFileName();
		//附件数组
		File[] upload = mail.getUpload();
		//显示发件人的名字
		String show_name = mail.getShow_name();
		//回复邮箱地址也就是显示发送者地址
		String recipient_email = mail.getRecipient_email();
		//正确邮件列表
		LinkedList<String> right_mailList = new LinkedList<String>();
		//错误邮件列表
		LinkedList<String> error_mailList = new LinkedList<String>();
		//验证邮箱的正则表达式
		java.util.regex.Pattern par = Pattern
				.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
		if (par.matcher(recipient_email).matches()) {
			//验证邮件列表
			for (int i = 0; i < tomails.length; i++) {
				String mailtmp = tomails[i];
				if (par.matcher(mailtmp).matches()) {
					right_mailList.add(mailtmp);
				} else {
					error_mailList.add(mailtmp);
				}
			}
			if (right_mailList.size()>0) {
				LinkedList<File> attachlist = new LinkedList<File>();
				if (upload != null) {
					for (int i = 0; i < upload.length; i++) {
						File file = upload[i];
						if (file != null) {
							attachlist.add(file);
						}
					}
				}
				MimeMessage message;
				InternetAddress[] address = new InternetAddress[right_mailList
						.size()];
				for (int i = 0; i < address.length; i++) {
					address[i] = new InternetAddress(right_mailList.get(i));
				}
				Properties props = new Properties();
				props.put("mail.smtp.host", stmp_host);
				props.put("mail.smtp.auth", "true");
				Session session = Session.getDefaultInstance(props, null);
				message = new MimeMessage(session);
				if (show_name == null || show_name.equals("")) {
					message.setFrom(new InternetAddress(recipient_email));
				} else {
					message.setFrom(new InternetAddress(recipient_email,
							show_name));
				}
				message.setRecipients(Message.RecipientType.TO, address);
				message.setSubject(mail.getSubject());
				message.setSentDate(mail.getSendDate());
				// 新建一个mimemultipart对象用来存放bodypart对象(事实上可以存放多个)
				MimeMultipart mm = new MimeMultipart();
				// 新建一个存放信件内容的bodypart对象
				BodyPart mdp = new MimeBodyPart();
				// 给bodypart对象设置内容和格式/编码方式
				mdp.setContent(mail.getContent(), mail.getContentType());
				mm.addBodyPart(mdp);
				// add the attachments
				for (int i = 0; i < attachlist.size(); i++) {
					// 新建一个存放附件的bodypart
					BodyPart bodyPart = new MimeBodyPart();
					FileDataSource fileDS = new FileDataSource(attachlist
							.get(i));
					DataHandler dh = new DataHandler(fileDS);
					bodyPart.setDataHandler(dh);
					// 加上这句将作为附件发送,否则将作为信件的文本内容
					bodyPart.setFileName(uploadFileName[i].toString());
					bodyPart.setDisposition(Part.ATTACHMENT);
					// mdp.setHeader("name", uploadFileName[i].toString());
					bodyPart.setHeader("Content-Type",
							"application/octet-stream;name="
									+ uploadFileName[i].toString());
					bodyPart.setHeader("X-Attachment-Id", uploadFileName[i]
							.toString());
					// 将含有附件的bodypart加入到mimemultipart对象中
					mm.addBodyPart(bodyPart);
				}
				// 把mm作为消息对象的内容
				message.setContent(mm);
				message.saveChanges();
				try {
					javax.mail.Transport transport = session
							.getTransport("smtp");
					transport.connect(stmp_host, mail.getStmp_username(), mail
							.getStmp_password());
					transport.sendMessage(message, message.getAllRecipients());
					transport.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
